1   /*
2    * Copyright (C) 2010 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ONE;
20  import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
21  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.collect.testing.Helpers;
25  import com.google.common.collect.testing.features.CollectionSize;
26  
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.NoSuchElementException;
30  import java.util.SortedSet;
31  
32  /**
33   * A generic JUnit test which tests operations on a SortedSet. Can't be
34   * invoked directly; please see {@code SortedSetTestSuiteBuilder}.
35   *
36   * @author Jesse Wilson
37   * @author Louis Wasserman
38   */
39  @GwtCompatible
40  public class SortedSetNavigationTester<E> extends AbstractSetTester<E> {
41  
42    private SortedSet<E> sortedSet;
43    private List<E> values;
44    private E a;
45    private E b;
46    private E c;
47  
48    @Override public void setUp() throws Exception {
49      super.setUp();
50      sortedSet = (SortedSet<E>) getSet();
51      values = Helpers.copyToList(getSubjectGenerator().getSampleElements(
52          getSubjectGenerator().getCollectionSize().getNumElements()));
53      Collections.sort(values, sortedSet.comparator());
54  
55      // some tests assume SEVERAL == 3
56      if (values.size() >= 1) {
57        a = values.get(0);
58        if (values.size() >= 3) {
59          b = values.get(1);
60          c = values.get(2);
61        }
62      }
63    }
64  
65    @CollectionSize.Require(ZERO)
66    public void testEmptySetFirst() {
67      try {
68        sortedSet.first();
69        fail();
70      } catch (NoSuchElementException e) {
71      }
72    }
73  
74    @CollectionSize.Require(ZERO)
75    public void testEmptySetLast() {
76      try {
77        sortedSet.last();
78        fail();
79      } catch (NoSuchElementException e) {
80      }
81    }
82  
83    @CollectionSize.Require(ONE)
84    public void testSingletonSetFirst() {
85      assertEquals(a, sortedSet.first());
86    }
87  
88    @CollectionSize.Require(ONE)
89    public void testSingletonSetLast() {
90      assertEquals(a, sortedSet.last());
91    }
92  
93    @CollectionSize.Require(SEVERAL)
94    public void testFirst() {
95      assertEquals(a, sortedSet.first());
96    }
97  
98    @CollectionSize.Require(SEVERAL)
99    public void testLast() {
100     assertEquals(c, sortedSet.last());
101   }
102 }